package ejbPart.session;

import ejbPart.entity.bankAccount.Account;
import ejbPart.session.stateless.testAccount.TestAccount;
import ejbPart.session.stateless.testAccount.TestAccountHome;

import java.util.Properties;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

public class TestStatelessBean {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        props.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");
        props.put(InitialContext.SECURITY_PRINCIPAL, "user");
        props.put(InitialContext.SECURITY_CREDENTIALS, "pass");
        props.put("jboss.naming.client.ejb.context", true);

        try {
            Context ctx = new InitialContext(props);
            final String path = "ejbPart/AccountTest!ejbPart.session.stateless.testAccount.TestAccountHome";
            Object obj = ctx.lookup(path);
            TestAccountHome testAccountHome = (TestAccountHome) PortableRemoteObject.narrow(obj, TestAccountHome.class);
            TestAccount testAccount = testAccountHome.create();
            System.out.println(testAccount.checkBalance());

            final String key = "123-456-3197";
            Account account = testAccount.findAccountById(key);
            account.deposite(-100);
            System.out.println("John Doe's balance = " + account.getBalance());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
